package com.ymatou.iapi.godfreightmgr.testcase;

import com.ymatou.iapi.godfreightmgr.parameter.*;
import com.ymatou.iapi.godfreightmgr.service.GetTemplateListBySellerIdCall;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.freight_template;
import com.ymttest.database.model.freight_template_detail;
import com.ymttest.database.sqlwapper.godfreight.freight_templateWapper;
import com.ymttest.database.sqlwapper.godfreight.freight_template_detailWapper;
import com.ymttest.utils.MapUtil;

import org.junit.*;
import org.junit.experimental.categories.Category;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*运费模板列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTemplateListBySellerId{
	private static GetTemplateListBySellerIdBean gettemplatelistbyselleridBean;
	private static GetTemplateListBySellerIdCall gettemplatelistbyselleridCall;
	private static freight_templateWapper templateWapper = new freight_templateWapper();
	private static freight_template_detailWapper detailWapper = new freight_template_detailWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("运费模板列表");
	}

	@Before
	public void caseUp() {
		gettemplatelistbyselleridBean=new GetTemplateListBySellerIdBean();
		gettemplatelistbyselleridCall=new GetTemplateListBySellerIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettemplatelistbyselleridCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(int sellerId){
		String ret = gettemplatelistbyselleridCall.getJsonObject().get("data").getAsJsonObject().toString();
		TemplateListBySellerIdVo templateList = (TemplateListBySellerIdVo) FormatUtil.GSONToObject(ret, TemplateListBySellerIdVo.class);
		Integer normalCount = templateWapper.selectCountBySellerId(sellerId, true);
		Integer totalCount = templateWapper.selectCountBySellerId(sellerId, false);
		Logger.verifyEquals(templateList.getTotalCount(),totalCount,"验证totalCount");
		Logger.verifyEquals(templateList.getNormalCount(),normalCount,"验证normalCount");

        templateList.getTemplateList().forEach(c->{
            int templateId = c.getTemplateId();
            FreightInfoVo tgtFreightInfoVo = c;
            freight_template template = templateWapper.selectByPrimaryKey(templateId);
            List<freight_template_detail> templateDetails = detailWapper.selectByTemplateId(templateId);

            Logger.debug(template);

            FreightInfoVo actFreightInfoVo = new FreightInfoVo();
            actFreightInfoVo.setSellerId(template.getSellerId());
            actFreightInfoVo.setShareTemplate(template.getIsShareTemplate());
            actFreightInfoVo.setTemplateId(templateId);
            actFreightInfoVo.setTemplateName(template.getTemplateName());
			actFreightInfoVo.setCurrency(template.getCurrency());
			if (template.getCurrency().equals("CYN")){
				actFreightInfoVo.setCurrencyName("人民币");
				actFreightInfoVo.setCurrencySymbol("¥");
			}else if (template.getCurrency().equals("USD")){
				actFreightInfoVo.setCurrencyName("美元");
				actFreightInfoVo.setCurrencySymbol("$");
			}

            List<freight_template_detail> mainFreightDetail = templateDetails.stream().filter(x -> x.getSaleMode() == 1 || x.getSaleMode() == 3).collect(Collectors.toList());
            List<freight_template_detail> agentFreightDetail = templateDetails.stream().filter(x -> x.getSaleMode() == 2).collect(Collectors.toList());

            CreateFreightDto mainFreight = new CreateFreightDto();
            mainFreight.setCalcModel(template.getDirectSaleCalcFeeMode());
            List<CreateFreightDetailDto> createFreightDetailDtos = mainFreightDetail.stream().map(x -> mapCreateFreightDetailDto(x)).collect(Collectors.toList());
            mainFreight.setTemplateItems(createFreightDetailDtos);
            if (!CollectionUtils.isEmpty(agentFreightDetail)){
                CreateFreightDto agentFreight = new CreateFreightDto();
                agentFreight.setCalcModel(template.getAgentSaleCalcFeeMode());
                List<CreateFreightDetailDto> createFreightDetailDtos1 = agentFreightDetail.stream().map(x -> mapCreateFreightDetailDto(x)).collect(Collectors.toList());
                agentFreight.setTemplateItems(createFreightDetailDtos1);
                actFreightInfoVo.setAgentFreight(agentFreight);
            }
            actFreightInfoVo.setMainFreight(mainFreight);

            Logger.debug("actFreightInfoVo:"+actFreightInfoVo);

            Map<String, Object> tgtMap = MapUtil.pojoToMap(tgtFreightInfoVo);
            tgtMap.remove("mainFreight");
            tgtMap.remove("agentFreight");
            Map<String, Object> actMap = MapUtil.pojoToMap(actFreightInfoVo);
            actMap.remove("mainFreight");
            actMap.remove("agentFreight");
            AssertService.assertResultEqual(tgtMap, actMap);

            CreateFreightDto tgtmainFreight = tgtFreightInfoVo.getMainFreight();
            CreateFreightDto actmainFreight = actFreightInfoVo.getMainFreight();
            Map<String, Object> tgtmainFreightMap = MapUtil.pojoToMap(tgtmainFreight);
            tgtmainFreightMap.remove("templateItems");
            Map<String, Object> actmainFreightMap = MapUtil.pojoToMap(actmainFreight);
            actmainFreightMap.remove("templateItems");
            AssertService.assertResultEqual(tgtmainFreightMap, actmainFreightMap);

            List<CreateFreightDetailDto> tgttemplateItems = tgtmainFreight.getTemplateItems();
            List<CreateFreightDetailDto> acttemplateItems = actmainFreight.getTemplateItems();
            tgttemplateItems.forEach(p->{
                CreateFreightDetailDto createFreightDetailDto = acttemplateItems.stream().filter(x -> x.getFirstFee().equals(p.getFirstFee())).findFirst().orElse(null);
                if (createFreightDetailDto!=null){
                    Map<String, Object> map = MapUtil.pojoToMap(p);
                    Map<String, Object> map1 = MapUtil.pojoToMap(createFreightDetailDto);
                    AssertService.assertResultEqual(map, map1);
                }
            });

            if (tgtFreightInfoVo.getAgentFreight()!=null){
                CreateFreightDto tgtmainFreight1 = tgtFreightInfoVo.getAgentFreight();
                CreateFreightDto actmainFreight1 = actFreightInfoVo.getAgentFreight();
                Map<String, Object> tgtmainFreightMap1 = MapUtil.pojoToMap(tgtmainFreight1);
                tgtmainFreightMap1.remove("templateItems");
                Map<String, Object> actmainFreightMap1 = MapUtil.pojoToMap(actmainFreight1);
                actmainFreightMap.remove("templateItems");
                AssertService.assertResultEqual(tgtmainFreightMap1, actmainFreightMap1);

                List<CreateFreightDetailDto> tgttemplateItems1 = tgtmainFreight1.getTemplateItems();
                List<CreateFreightDetailDto> acttemplateItems1 = actmainFreight1.getTemplateItems();
                tgttemplateItems1.forEach(p->{
                    CreateFreightDetailDto createFreightDetailDto = acttemplateItems1.stream().filter(x -> x.getFirstFee().equals(p.getFirstFee())).findFirst().orElse(null);
                    if (createFreightDetailDto!=null){
                        Map<String, Object> map = MapUtil.pojoToMap(p);
                        Map<String, Object> map1 = MapUtil.pojoToMap(createFreightDetailDto);
                        AssertService.assertResultEqual(map, map1);
                    }
                });
            }
        });
	}

    public CreateFreightDetailDto mapCreateFreightDetailDto(freight_template_detail templateDetail){
        CreateFreightDetailDto createFreightDetailDto = new CreateFreightDetailDto();
        createFreightDetailDto.setDefaultTemplate(templateDetail.getIsDefault());
        createFreightDetailDto.setDeliveryRegion(StringUtils.isEmpty(templateDetail) ? Arrays.asList() : Arrays.asList(templateDetail.getDeliveryCity().split(",")));
        createFreightDetailDto.setFirstFee(templateDetail.getFirstFee());
        createFreightDetailDto.setAddFee(templateDetail.getAddFee());
        if (templateDetail.getCalcFeeMode()==1){
            createFreightDetailDto.setFirstNum(templateDetail.getFirstNum().intValue());
            createFreightDetailDto.setAddNum(templateDetail.getAddNum().intValue());
        }else if (templateDetail.getCalcFeeMode()==2){
            createFreightDetailDto.setFirstWeight(templateDetail.getFirstNum());
            createFreightDetailDto.setAddWeight(templateDetail.getAddNum());
        }
        return createFreightDetailDto;
    }

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateListBySellerId_01() {
		Logger.start(true,"获取运费模板列表，OnlyDefault为true，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 1, 1);

			CreateFreightDto createFreightDto = new CreateFreightDto();
			createFreightDto.setCalcModel(1);

			CreateFreightDetailDto createFreightDetail = new CreateFreightDetailDto();
			createFreightDetail.setDefaultTemplate(true);//是否默认运费模板
			createFreightDetail.setFirstNum(1);//首件数量
			createFreightDetail.setAddNum(2);//续件数量
			createFreightDetail.setFirstFee(new BigDecimal(10));//首件/首重运费
			createFreightDetail.setAddFee(new BigDecimal(5));//续件 / 续重 运费

			CreateFreightDetailDto createFreightDetail1 = new CreateFreightDetailDto();
			createFreightDetail1.setDefaultTemplate(false);//是否默认运费模板
			createFreightDetail1.setFirstNum(1);//首件数量
			createFreightDetail1.setDeliveryRegion(Arrays.asList("北京市"));
			createFreightDetail1.setAddNum(2);//续件数量
			createFreightDetail1.setFirstFee(new BigDecimal(15));//首件/首重运费
			createFreightDetail1.setAddFee(new BigDecimal(5));//续件 / 续重 运费

			List<CreateFreightDetailDto> templateItems = new ArrayList<>();
			templateItems.add(createFreightDetail);
			templateItems.add(createFreightDetail1);
			createFreightDto.setTemplateItems(templateItems);//运费明细内容

			createFreightTemplateBean.setMainFreight(createFreightDto);
			GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);

			gettemplatelistbyselleridBean.setSellerId(createmchBean.getMchId());
			gettemplatelistbyselleridBean.setOnlyDefault(true);
			gettemplatelistbyselleridCall.setData(gettemplatelistbyselleridBean);
			gettemplatelistbyselleridCall.callService();			
			Logger.verifyEquals("200",gettemplatelistbyselleridCall.getString("code"),"验证code");
			verifyDB(createmchBean.getMchId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTemplateListBySellerId_02() {
		Logger.start(true,"获取运费模板列表，OnlyDefault为false，成功");
		try {
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel();
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			CreateFreightTemplateBean createFreightTemplateBean = GodfreightmgrService.initCreateFreightTemplate(createmchBean.getMchId(), 1, 1);

			CreateFreightDto createFreightDto = new CreateFreightDto();
			createFreightDto.setCalcModel(1);

			CreateFreightDetailDto createFreightDetail = new CreateFreightDetailDto();
			createFreightDetail.setDefaultTemplate(true);//是否默认运费模板
			createFreightDetail.setFirstNum(1);//首件数量
			createFreightDetail.setAddNum(2);//续件数量
			createFreightDetail.setFirstFee(new BigDecimal(10));//首件/首重运费
			createFreightDetail.setAddFee(new BigDecimal(5));//续件 / 续重 运费

			CreateFreightDetailDto createFreightDetail1 = new CreateFreightDetailDto();
			createFreightDetail1.setDefaultTemplate(false);//是否默认运费模板
			createFreightDetail1.setFirstNum(1);//首件数量
			createFreightDetail1.setDeliveryRegion(Arrays.asList("北京市"));
			createFreightDetail1.setAddNum(2);//续件数量
			createFreightDetail1.setFirstFee(new BigDecimal(15));//首件/首重运费
			createFreightDetail1.setAddFee(new BigDecimal(5));//续件 / 续重 运费

			List<CreateFreightDetailDto> templateItems = new ArrayList<>();
			templateItems.add(createFreightDetail);
			templateItems.add(createFreightDetail1);
			createFreightDto.setTemplateItems(templateItems);//运费明细内容

			createFreightTemplateBean.setMainFreight(createFreightDto);
			GodfreightmgrService.CreateFreightTemplate(createFreightTemplateBean);

			gettemplatelistbyselleridBean.setSellerId(createmchBean.getMchId());
			gettemplatelistbyselleridBean.setOnlyDefault(false);
			gettemplatelistbyselleridCall.setData(gettemplatelistbyselleridBean);
			gettemplatelistbyselleridCall.callService();
			Logger.verifyEquals("200",gettemplatelistbyselleridCall.getString("code"),"验证code");
			verifyDB(createmchBean.getMchId());
		} catch (Exception e) {
			Logger.fail(e);
		}

	}


}